package com.example.trafficexam2.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;

import com.example.trafficexam2.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/11/24/024.
 */

public class ViewFragement extends AppCompatActivity {
    List<android.support.v4.app.Fragment> fragmentList = new ArrayList<>();
    private ViewFragment1 viewFragment1;
    private ViewFragment2 viewFragment2;
    private ViewFragment3 viewFragment3;
    private ViewFragment4 viewFragment4;
    private ViewFragment5 viewFragment5;
    private ViewFragment6 viewFragment6;
    private ViewPager viewPager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_layout);
        initView();
    }

    private void initView() {
        viewFragment1 = new ViewFragment1();
        viewFragment2 = new ViewFragment2();
        viewFragment3 = new ViewFragment3();
        viewFragment4 = new ViewFragment4();
        viewFragment5 = new ViewFragment5();
        viewFragment6 = new ViewFragment6();
        fragmentList.add(viewFragment1);
        fragmentList.add(viewFragment2);
        fragmentList.add(viewFragment3);
        fragmentList.add(viewFragment4);
        fragmentList.add(viewFragment5);
        fragmentList.add(viewFragment6);
        viewPager = findViewById(R.id.view_viewpager);
        FragmentPagerAdapter fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };
        viewPager.setAdapter(fragmentPagerAdapter);
        final ImageView imageView1 = findViewById(R.id.data_analysis_viewPage_1);
        final ImageView imageView2 = findViewById(R.id.data_analysis_viewPage_2);
        final ImageView imageView3 = findViewById(R.id.data_analysis_viewPage_3);
        final ImageView imageView4 = findViewById(R.id.data_analysis_viewPage_4);
        final ImageView imageView5 = findViewById(R.id.data_analysis_viewPage_5);
        final ImageView imageView6 = findViewById(R.id.data_analysis_viewPage_6);
        imageView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(0);
            }
        });
        imageView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(1);
            }
        });
        imageView3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(2);
            }
        });
        imageView4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(3);
            }
        });
        imageView5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(4);
            }
        });
        imageView6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(5);
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        imageView1.setImageResource(R.drawable.page_indicator_focused);
                        imageView2.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView3.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView4.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView5.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView6.setImageResource(R.drawable.page_indicator_unfocused);
                        break;
                    case 1:
                        imageView1.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView2.setImageResource(R.drawable.page_indicator_focused);
                        imageView3.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView4.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView5.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView6.setImageResource(R.drawable.page_indicator_unfocused);
                        break;
                    case 2:
                        imageView1.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView2.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView3.setImageResource(R.drawable.page_indicator_focused);
                        imageView4.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView5.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView6.setImageResource(R.drawable.page_indicator_unfocused);
                        break;
                    case 3:
                        imageView1.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView2.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView3.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView4.setImageResource(R.drawable.page_indicator_focused);
                        imageView5.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView6.setImageResource(R.drawable.page_indicator_unfocused);

                        break;
                    case 4:
                        imageView1.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView2.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView3.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView4.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView5.setImageResource(R.drawable.page_indicator_focused);
                        imageView6.setImageResource(R.drawable.page_indicator_unfocused);
                        break;
                    case 5:
                        imageView1.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView2.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView3.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView4.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView5.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView6.setImageResource(R.drawable.page_indicator_focused);
                        break;
                    case 6:
                        imageView1.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView2.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView3.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView4.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView5.setImageResource(R.drawable.page_indicator_unfocused);
                        imageView6.setImageResource(R.drawable.page_indicator_unfocused);
                        break;
                }
            }
        });
    }

}
